package com.sdc.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.sdc.control.ControleUsuario;

/**
 * Servlet implementation class RelatoriosServlet
 */
public class RelatoriosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(ProdutosServlet.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RelatoriosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

		ControleUsuario cu = new ControleUsuario();
		RequestDispatcher dispatcher;

		
		String p1 = "";
		String p2 = "";
		List l;
		if (request.getParameter("data_inicio") != null) {
			p1 = request.getParameter("data_inicio");
		}
		if (request.getParameter("data_fim") != null) {
			p2 = request.getParameter("data_fim");
		}
		if(p1.equals("") && p2.equals("")){
			l = cu.listarAcessosByUsuario();
			request.setAttribute("acessosByUsuario", l);
			log.info("Usuarios obtidos [" + l.size() + "]");
			dispatcher = request.getRequestDispatcher("jsp/relatorios/acessos.jsf");
		}else{
			String[] user_param = { p1, p2 };
			l = cu.listarAcessosByUsuario(user_param);	
			request.setAttribute("acessosByUsuario", l);
			log.info("Usuarios obtidos [" + l.size() + "]");
			
			dispatcher = request.getRequestDispatcher("jsp/relatorios/acessos_lista.jsf");
		}
		
		dispatcher.forward(request, response);

	}

}
